﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsFormsApplication2.CLASS
{
    public sealed class DBTools
    {
        public enum Authentication
        {
            Windows = 1,
            Sql = 2,

        }
        public enum _Directions
        {
            Input = 1,
            InputOutput = 3,
            Output = 2,
            ReturnValue = 6
        }

        public static readonly SqlConnection _Conn = new SqlConnection();
        public static readonly SqlTransaction _Trans = null;

        public static readonly SqlCommand _Comm = new SqlCommand("", _Conn, _Trans);
        public static readonly SqlDataAdapter _DA = new SqlDataAdapter(_Comm);
        // public static readonly SqlDataReader _Reader =new SqlDataReader();
        //sqlConnection = SqlConnection("data source=bondugula;
        //                          initial catalog=Northwind;
        //                          integrated security=SSPI;
        //                          persist security info=False;
        //                          Trusted_Connection=Yes");




        public static bool ConnectDB(string ServerName, string DBname, string UserName, string Password, Authentication conType)
        {
            string connectionSql = String.Format("Data Source={0};Initial Catalog= {1};Persist Security Info=True;User ID= {2};password= {3};Connect Timeout=0 ", ServerName, DBname, UserName, Password);
            string connectionWin = String.Format("Data Source={0};Initial Catalog= {1}; integrated security=SSPI;persist security info=False;Trusted_Connection=Yes;Connect Timeout=0 ", ServerName, DBname);
            string sqlconnectionstr = "";

            if (conType == Authentication.Sql)
            {
                sqlconnectionstr = connectionSql;
            }
            else
                sqlconnectionstr = connectionWin;




            try
            {
                // Don't initialize this variable here.
                using (SqlConnection connection = new SqlConnection(sqlconnectionstr))
                {

                    connection.Open();
                    _Conn.ConnectionString = sqlconnectionstr;

                    return true;


                }


            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);

            }
            // Error: Use of unassigned local variable 'x'.
            return false;




        }
        public static DataTable selecQryTmp(string qry1)
        {
            //select *from student

            DataTable temptable = new DataTable();
            _Comm.CommandType = CommandType.Text;
            _Comm.CommandText = qry1;

            _DA.Fill(temptable);
            temptable.AcceptChanges();
            return temptable;
        }
    }
}